package com.yksj.healthtalk.ui.salon;

import java.util.ArrayList;

import org.handmark.pulltorefresh.library.PullToRefreshListView;

import sfs2x.client.core.SFSEvent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.FriendAdapter;
import com.yksj.healthtalk.adapter.FriendAdapter.onClickFriendAttentionListener;
import com.yksj.healthtalk.adapter.FriendAdapter.onClickFriendHeadListener;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.MessagePushService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.friend.FriendInformUI;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.PersonInfoUtil;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 沙龙 服务人员列表
 * @author jack_tang
 *
 */
public class ServiceMembersActivity extends BaseFragmentActivity implements OnClickListener, OnItemClickListener,onClickFriendAttentionListener, onClickFriendHeadListener{
	
	private PullToRefreshListView mPullToRefreshListView;
	private ListView lv;
	private CustomerInfoEntity cacheCustomerInfoEntity;
	private AppData mAppData;
	
	BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (action.equals(MessagePushService.ACTION_COLLECT_FRIEND)) {
				LodingFragmentDialog.dismiss(getSupportFragmentManager());
				String result = intent.getStringExtra("result");
				if (result.equals("0")) {
					ToastUtil.showShort(getApplicationContext(), R.string.groupNewFail);
				} else {
					FriendHttpUtil.requestAttentionTofriendsResult(
							getApplicationContext(), cacheCustomerInfoEntity, mAppData);
					adapter.notifyDataSetChanged();
				}
			} else if (action.equals(SFSEvent.CONNECTION_LOST)) {
			}
		}
	};
	private FriendAdapter adapter;
	private IntentFilter filter;
	private ArrayList<CustomerInfoEntity> entities;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.service_member);
		mAppData = HTalkApplication.getAppData();
		initWidget();
		initData();
	}

	private void initData() {
		titleTextV.setText(R.string.server_member_list);
		titleLeftBtn.setText(R.string.back);
		titleLeftBtn.setOnClickListener(this);
		
		entities = getIntent().getParcelableArrayListExtra("list");
		if (entities != null) {
			adapter = new FriendAdapter(getApplicationContext(), entities);
			adapter.setonClickFriendAttentionListener(this);
			adapter.setonClickFriendHeadListener(this);
			lv.setAdapter(adapter);
			}
		
	}

	private void initWidget() {
		initTitle();
		lv = (ListView) this.findViewById(R.id.hall);
		lv.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		try {
			CustomerInfoEntity entity = entities.get(position);
			if (entity.isDoctor()) {
				onClickFriendHead(entity, position);
			}else {
				FriendHttpUtil.onItemClick(this, entity);
			}
		} catch (Exception e) {
			
		}
	}
	
	@Override
	protected void onStart() {
		filter = new IntentFilter(MessagePushService.ACTION_FRIENDLIST);
		filter.addAction(MessagePushService.ACTION_COLLECT_FRIEND);
		filter.addAction(SFSEvent.CONNECTION_LOST);
		registerReceiver(receiver, filter);
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		unregisterReceiver(receiver);
		super.onStop();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		}
	}

	@Override
	public void onClickFriendHead(CustomerInfoEntity entity, int positon) {
		PersonInfoUtil.choiceActivity(entity.getId(), this,String.valueOf(entity.getRoldid()));
	}

	@Override
	public void onClickFriendAttention(int attentioned,
			CustomerInfoEntity entity, int positon) {
		cacheCustomerInfoEntity = FriendHttpUtil.requestAttentionTofriends(this,
				null, entity);
	}
	

}
